#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
__title__ = ''
__author__ = 'tinxi'
__mtime__ = '2018/6/30'
# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
    
'''


# 这是一个表单验证获取错误信息的方法类, 为了便于多个类进行复用故抽取出来,需要用他的直接继承它即可
# print('错误:', form.errors)
# print('错误类型是:', type(form.errors))
# data = form.errors.get_json_data()  # form.errors.get_json_data() 返回一个dic类型
# {'telephone': [{'message': '必须输入手机号码!', 'code': 'required'}],
#  'username': [{'message': '必须输入用户名!', 'code': 'required'}],
#  'password1': [{'message': '必须输入密码!', 'code': 'required'}],
#  'password2': [{'message': '必须输入密码!', 'code': 'required'}],
#  'sms_captcha': [{'message': '必须输入验证码!', 'code': 'required'}],
#  'img_captcha': [{'message': '必须输入手机验证码!', 'code': 'required'}]}
# print('错误是:', data)

class FormsMixin(object):
    # 默认展示一条错误信息, 传递了all参数后则暂时所有信息
    def get_errors(self, all_message=0):
        if all_message:
            if hasattr(self, 'errors'):
                message_list = []
                errors = self.errors.get_json_data()
                flag = 1
                while flag:
                    if errors:
                        error_tuple = errors.popitem()
                        error_list = error_tuple[1]
                        message = error_list[0]['message']
                        message_list.append(message)
                        print(message)
                    else:
                        flag = 0
                return message_list
        else:
            if hasattr(self, 'errors'):
                errors = self.errors.get_json_data()
                if errors:
                    error_tuple = errors.popitem()  # 随机取一条错误信息
                    error_list = error_tuple[1]
                    message = error_list[0]['message']
                    print(message)
                return message
